package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.constant.MessageConstant;
import com.atguigu.entity.PageResult;
import com.atguigu.entity.QueryPageBean;
import com.atguigu.entity.Result;
import com.atguigu.pojo.TravelGroup;
import com.atguigu.pojo.TravelItem;
import com.atguigu.service.TravelGroupService;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/TravelGroup")
public class TravelGroupController {

    @Reference
    TravelGroupService travelGroupService;

    @PostMapping("/getGroupItem")
    public Result getGroupItem(){

        try {
            List<TravelItem> travelItems = travelGroupService.getGroupItem();
            return new Result(true, MessageConstant.QUERY_TRAVELITEM_SUCCESS,travelItems);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_TRAVELITEM_FAIL);
        }
    }
    @PostMapping("/add")
    public Result add(@RequestBody TravelGroup travelGroup, Integer[] travelItemIds){
        try {
            travelGroupService.add(travelGroup,travelItemIds);
            return new Result(true,MessageConstant.ADD_TRAVELGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_TRAVELGROUP_FAIL);
        }
    }

    @PostMapping("/get")
    public PageResult get(@RequestBody QueryPageBean queryPageBean){

       PageResult pageResult = travelGroupService.get(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());

        return pageResult;

    }

    @GetMapping("/delete")
    public Result delete(Integer id){
        try {
            int i = travelGroupService.selectGroupItem(id);
            if(i > 0){
                return new Result(false,"存在关联关系，删除失败");
            }else{
                travelGroupService.delete(id);
                return new Result(true,MessageConstant.DELETE_TRAVELGROUP_SUCCESS);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_TRAVELGROUP_FAIL);
        }
    }

    @GetMapping("/getGroupById")
    public Result getGroupById(Integer id){
        try {
            List<TravelItem> groupItem = travelGroupService.getGroupItem();
            TravelGroup travelGroup = travelGroupService.selectGroupId(id);
            Map<String,Object> map = new HashMap<>();
            map.put("groupItem",groupItem);
            map.put("travelGroup",travelGroup);
            return new Result(true,MessageConstant.QUERY_TRAVELGROUP_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_TRAVELGROUP_FAIL);
        }
    }

    @GetMapping("/findTravelItemIdByTravelgroupId")
    public Result findTravelItemIdByTravelgroupId(Integer id){
        try {
            List<Integer> list = travelGroupService.findTravelItemIdByTravelgroupId(id);
            return new Result(true,"查询关联数据成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询关联数据失败");
        }
    }

    @PostMapping("/update")
    public Result update(@RequestBody TravelGroup travelGroup,Integer[] travelItemIds){
        try {
            travelGroupService.update(travelGroup,travelItemIds);
            return new Result(true,MessageConstant.EDIT_TRAVELGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_TRAVELGROUP_FAIL);
        }
    }

}
